<?php
namespace app\app\controller;
use think\Db;
class Order extends Common
{
    public function get_detail()
    {
        $uid = token_decode(req('token'));
        $order_id = req('order_id');
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid) || empty($order_id)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            $fields = 'order_id,title,lng,lat,address,name,phone,remark,seller_id,last_price,express_charge,sum_price,new_user_reduction,money_off_text,money_off,red_packet,status,advance,advance_receive,payment,time,pay_time,express_receive_time,seller_receive_time,express_time,complete_time,back_time,cancel_time,express_id';
            $data = Db::table('order')->field($fields)->where(['order_id'=>$order_id,'uid'=>$uid])->find();
            $seller = Db::table('seller')->where('id',$data['seller_id'])->find();
            $data['seller_name'] = $seller['name'];
            $data['seller_headimg'] = $seller['headimg'];
            $data['seller_business'] = $seller['business'];
            $data['seller_advance'] = $seller['advance'];
            $data['seller_label'] = $seller['label'];
            $data['seller_lng'] = $seller['lng'];
            $data['seller_lat'] = $seller['lat'];
            $goods_total = 0;
            $goods_price = 0;
            $box_charge = 0;
            $goods = Db::table('order_detail')->field('goods_id,number,price,box_charge,title,image,service')->where('order_id',$data['order_id'])->select();
            $data['goods_total'] = (string)$goods_total;
            $data['goods_price'] = (string)$goods_price;
            $data['goods_price'] = (string)$goods_price;
            $data['box_charge'] = (string)$box_charge;

            if(!empty($data['express_id'])){
                $express = Db::table('express')->where('id',$data['express_id'])->find();
                $data['express_headimg'] = $express['headimg'];
                $data['express_name'] = $express['name'];
                $data['express_phone'] = $express['phone'];
                $data['express_lng'] = $express['lng'];
                $data['express_lat'] = $express['lat'];
            }else{
                $data['express_headimg'] = '';
                $data['express_phone'] = '';
                $data['express_name'] = '';
                $data['express_lng'] = '';
                $data['express_lat'] = '';
            }
            $data['goods'] = $goods;
            $msg['data'] = $data;
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }
		
		public function express_status_callback(){
			$order_id = req('order_id');
			$status = req('order_status');
			//订单状态(待接单＝1 待取货＝2 配送中＝3 已完成＝4 已取消＝5 已过期＝7 指派单=8 妥投异常之物品返回中=9 妥投异常之物品返回完成=10 创建达达运单失败=1000 可参考文末的状态说明）
			$order = Db::table('order')->where('order_id',$order_id)->find();
			if($order['status'] != $status){
				$update['status'] = $status;
				switch($status){
					case 2:
						$update['express_id'] = $uid;
            $update['express_receive_time'] = time();
						push(md5($order['uid']), json_encode(['type' => 2, 'message' => '您的订单已被配送员接单', 'parameters' => $order_id]), 'user');
						push(md5($order['seller_id']), json_encode(['type' => 2, 'message' => '您的订单已被配送员接单,请等待配送员上门取货', 'parameters' => $order_id]), 'seller');
						break;
					case 3:
						$update['express_time'] = time();
						push(md5($order['uid']), json_encode(['type' => 2, 'message' => '您的订单已被配送员送出', 'parameters' => $order_id]), 'user');
						break;
					case 4:
						//完成订单
						$update['complete_time'] = time();

						//给商家和配送员加钱
						$seller = Db::table('seller')->field('cut,aid,volume,comode')->where('id', $order['seller_id'])->find();
						if($seller['comode'] == 0){
							if(!empty($order['money_off'])){
								$order['goods_price'] = $order['goods_price'] - $order['seller_money_off'];
							}
							$order['goods_price'] = $order['goods_price'] + $order['box_charge'];
							$seller_money = $order['goods_price'] - ($order['goods_price'] * $seller['cut'] / 100);
												}else{
							$order['goods_price'] = $order['goods_price'] + $order['box_charge'];
							$seller_money = $order['goods_price'] - ($order['goods_price'] * $seller['cut'] / 100);
							if(!empty($order['money_off'])){
								$order['goods_price'] = $order['goods_price'] - $order['seller_money_off'];
							}
						}
						$seller_money = round($seller_money,2);
						Db::table('seller')->where('id', $order['seller_id'])->setInc('money', $seller_money);
						Db::table('earning')->insert(['order_id' => $order_id, 'uid' => $order['seller_id'], 'money' => $seller_money, 'time' => time(), 'type' => 'seller']);

						// $express = Db::table('express')->where('id', $uid)->find();
						// $express_money = $order['express_charge'] - ($order['express_charge'] * $express['cut'] / 100);
						// Db::table('express')->where('id', $uid)->setInc('money', $express_money);
						// Db::table('earning')->insert(['order_id' => $order_id, 'uid' => $uid, 'money' => $order['express_charge'], 'time' => time(), 'type' => 'express']);

						//push用户
						push(md5($order['uid']), json_encode(['type' => 2, 'message' => '您的订单已完成', 'parameters' => $order_id]), 'user');

						//自动评价预启动
						Db::table('evaluate')->insert(['order_id'=>$order_id,'seller_id'=>$order['seller_id'],'uid'=>$order['uid'],'star'=>'5.0','text'=>'系统默认好评','time'=>time(),'complete_time'=>time()]);
						Db::table('evaluate')->insert(['order_id'=>$order_id,'express_id'=>$uid,'uid'=>$order['uid'],'star'=>'5.0','text'=>'系统默认好评','time'=>time(),'complete_time'=>time()]);

						//push商家
						push(md5($order['seller_id']), json_encode(['type' => 2, 'message' => '订单已完成，本次收益'.$seller_money.'元', 'parameters' => $order_id]), 'seller');

						//销量
						$volume = $seller['volume'];
						$goods = Db::table('order_detail')->where('order_id', $order['order_id'])->select();
						foreach ($goods as $k => $v) {
								$volume += $v['number'];
								$goods_volume = (int)Db::table('service')->where('id',$v['goods_id'])->value('volume');
								Db::table('service')->where(['id' => $v['goods_id'], 'uid' => $v['seller_id']])->update(['volume'=> $goods_volume+$v['number'],'volume_date'=>date('Ym')]);
						}
						Db::table('seller')->where('id',$order['seller_id'])->update(['volume'=>$volume,'volume_date'=>date('Ym')]);

						$user_order_count = Db::table('order')->where(['order_id'=>['<>',$order_id],'uid'=>$order['uid'],'status'=>5])->count();
						$invitation = Db::table('user')->where(['id'=>$order['uid'],'status'=>1])->value('invite');
						//邀请送好礼
						if (!empty($invitation) && $user_order_count == 0) {
							$where['aid'] = $order['aid'];
							$where['status'] = 1;
							$where['title'] = '邀请送好礼';
							$active = Db::table('active')->where($where)->find();
							if (!empty($active['redpacket'])) {
								$this->grant_redpacket($invitation,$active['redpacket'],'邀请红包',0);
							}
						}
						//三级分销
						$setting = Db::table("setting")->where(['id'=>1])->find();
						//一级
						if(!empty($invitation) && $setting['level_1']>0){
							$commision_money = round($order['last_price']/100*$setting['level_1'],2);
							if(Db::table("user")->where(['id'=>$invitation,'status'=>1])->setInc('money', $commision_money)){
								Db::table('earning')->insert(['order_id' => $order_id, 'uid' => $invitation, 'money' => $commision_money, 'time' => time(), 'type' => 'user']);
								$commision = $commision + $commision_money;
							}
							//二级
							if(!empty($invitation) && $setting['level_2']>0){
								$invitation = Db::table('user')->where(['id'=>$invitation,'status'=>1])->value('invite');
								$commision_money = round($order['last_price']/100*$setting['level_2'],2);
								if(Db::table("user")->where(['id'=>$invitation,'status'=>1])->setInc('money', $commision_money)){
									Db::table('earning')->insert(['order_id' => $order_id, 'uid' => $invitation, 'money' => $commision_money, 'time' => time(), 'type' => 'user']);
									$commision = $commision + $commision_money;
									//三级
									if(!empty($invitation) && $setting['level_3']>0){
										$invitation = Db::table('user')->where(['id'=>$invitation,'status'=>1])->value('invite');
										$commision_money = round($order['last_price']/100*$setting['level_3'],2);
										if(Db::table("user")->where(['id'=>$invitation,'status'=>1])->setInc('money', $commision_money)){
											Db::table('earning')->insert(['order_id' => $order_id, 'uid' => $invitation, 'money' => $commision_money, 'time' => time(), 'type' => 'user']);
											$commision = $commision + $commision_money;
										}
									}
								}
							}
						}
						//算平台钱+不算配送费
						//$update['system_money'] = $order['last_price']-$seller_money-$express_money;
						$update['system_money'] = $order['last_price']-$seller_money-$commision;
						break;
				}
				Db::table('order')->where('order_id',$order_id)->update($update);
			}
			return "这里是回调接口";
		}

}